package com.base.cn.platform.os.controller.manage.help;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.help.SysHelpCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 后台帮助中心
 *
 * @author s.li
 * @create 2018-04-02-10:04
 */
@Controller
@RequestMapping("/admin/manage/help")
public class AdminHelpController extends AdminBaseController {

    @Autowired
    private SysHelpCenterService sysHelpCenterService;

    /**
     * 初始化帮助文档页面
     * @return 操作页面
     */
    @RequestMapping("/helpCenter")
    public String helpCenter(){
        return resultPage("/manage/help/help_center");
    }

    /**
     * 查询帮助文档数据列表
     * @return Map<String,Object>
     */
    @RequestMapping("/ajaxHelpCenter")
    @ResponseBody
    public Map<String,Object> ajaxHelpCenter(){
        Map<String,Object> params = new HashMap<>();
        List<Map<String,Object>> sysHelpCenterList = sysHelpCenterService.findSysHelpCenter(params,false);
        return ResultUtil.SUCCESS("帮助文档列表查询成功",sysHelpCenterList);
    }

    /**
     * 初始化添加/修改页面
     * @param model
     * @param id
     * @param parentId
     * @return
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id",required = false,defaultValue = "0")BigDecimal id,
                                  @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId,
                                  @RequestParam(value = "status",required = false,defaultValue = "1") int status){
        Map<String,Object> sysHelpCenter = null;
        if(id!=null && id.compareTo(new BigDecimal(0)) >0){
            sysHelpCenter = sysHelpCenterService.findHelpCenterById(id);
        }
        if(sysHelpCenter==null){
            sysHelpCenter = new HashMap<>();
            sysHelpCenter.put("parentId",parentId);
            sysHelpCenter.put("status",status);
        }
        model.addAttribute("sysHelpCenter",sysHelpCenter);
        return resultPage("/manage/help/save_help");
    }

    /**
     * 保存帮助数据文档
     * @param sysHelpCenter
     * @return Map<String,Object>
     */
    @RequestMapping("/saveHelpCenter")
    @ResponseBody
    public Map<String,Object> saveHelpCenter(@RequestBody Map<String,Object> sysHelpCenter){
        Map<String,Object> resultMap = sysHelpCenterService.saveHelpCenter(sysHelpCenter);
        resultMap.put("parentId",sysHelpCenter.get("parentId"));
        return resultMap;
    }

    /**
     * 删除帮助文档数据内容
     * @param ids 帮助文档内容
     * @return  Map<String,Object>
     */
    @RequestMapping("/deleteHelpCenter")
    @ResponseBody
    public Map<String,Object> deleteHelpCenter(@RequestParam("ids") String ids){
        return sysHelpCenterService.deleteHelpCenter(ids);
    }

    /**
     * 修改状态
     * @param ids ID串
     * @param status 状态，1正常，2隐藏
     * @return Map<String,Object>
     */
    @RequestMapping("/updateHelpCenterStatus")
    @ResponseBody
    public Map<String,Object> updateHelpCenterStatus(@RequestParam("ids") String ids,
                                                     @RequestParam("status") int status){
        return sysHelpCenterService.updateHelpStatus(ids,status);
    }

    /**
     * 跳转帮助中心
     * @param model
     * @return
     */
    @RequestMapping("/showHelpCenter")
    public String showHelpCenter(Model  model){
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        params.put("showClients",1);
        List<Map<String,Object>> helpList = sysHelpCenterService.findPageHelpList(params,false);
        model.addAttribute("helpList",helpList);
        return resultPage("/manage/help/show-help-center");
    }

    /**
     * 获取显示的内容
     * @param helpId
     * @return
     */
    @RequestMapping("/showHelpContext")
    @ResponseBody
    public Map<String,Object> showHelpContext(@RequestParam("helpId") BigDecimal helpId){
        Map<String,Object> helpContext = sysHelpCenterService.findHelpCenterById(helpId);
        return ResultUtil.SUCCESS(helpContext);
    }
}
